package ytdp.careagle.com.ytdp.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import ytdp.careagle.com.ytdp.activity.EvaluateResultActivity;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.entity.BaseConfig;
import ytdp.careagle.com.ytdp.entity.ModelRelationship;
import ytdp.careagle.com.ytdp.entity.ProvinceEntity;
import ytdp.careagle.com.ytdp.entity.ValuationQuery;
import ytdp.careagle.com.ytdp.entity.VehicleSeries;
import ytdp.careagle.com.ytdp.mvp.mode.CarEvaluateModel;
import ytdp.careagle.com.ytdp.mvp.view.ICarEvaluateView;
import ytdp.careagle.com.ytdp.net.MyObserver;
import ytdp.careagle.com.ytdp.net.NetWorks;
import ytdp.careagle.com.ytdp.net.Result;
import ytdp.careagle.com.ytdp.net.Results;
import ytdp.careagle.com.ytdp.utils.SPUtils;
import ytdp.careagle.com.ytdp.utils.SerializeableUtils;
import ytdp.careagle.com.ytdp.utils.Utils;

/**
 * Created by admin on 2017/9/21.
 */

public class CarEvaluatePresenter {
    private ICarEvaluateView view;
    private CarEvaluateModel model;

    public CarEvaluatePresenter(ICarEvaluateView view) {
        this.view = view;
        model = new CarEvaluateModel();
    }

    public void getCityData(Context context) {
        final String path = G.getFileCacheDirPath() + "/inspectStandard.txt";
        List<ProvinceEntity> provinceEntities = SerializeableUtils.deserializeData(path);
        if (provinceEntities == null) {
            view.showProgress("请稍等");
            model.getLocalProvinceCity(context, G.getHeader(context), new MyObserver<Results<ProvinceEntity>>() {
                @Override
                public void onNext(Results<ProvinceEntity> provinceEntity) {
                    if (provinceEntity.getCode() == G.REQUEST_OK) {
                        List<ProvinceEntity> provinceEntities = provinceEntity.getData();
                        view.selectCity((Serializable) provinceEntities);
                        SerializeableUtils.serializeData(provinceEntities, path);
                    } else {
                        view.showMessage(provinceEntity.getMessage());
                    }
                    view.hideProgress();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    view.showMessage("访问不到服务器");
                    view.hideProgress();
                }
            });
        } else {
            view.selectCity((Serializable) provinceEntities);
        }
    }

    public void startEvaluate(final Context context) {
        ModelRelationship.ChildrenBean childrenBean = view.getCarModel();
        String mileage = view.getMileage();
        String cityCode = view.getCityCode();
        String provinceCode = view.getProvinceCode();
        String cityName = view.getCityName();
        String provinceName = view.getProvinceName();
        String firstRegDate = view.getFirstRegDate();
        String[] split = firstRegDate.split("-");
        if (Integer.valueOf(split[1]) < 10) {
            firstRegDate = split[0] + "-0" + split[1];
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("modelCode", childrenBean.getCode());
        map.put("mileage", mileage);
        map.put("cityCode", cityCode);
        map.put("firstRegDate", firstRegDate);
        map.put("modelName", childrenBean.getName());
        map.put("carSeriesCode", childrenBean.getSeriesCode());
        map.put("carSeriesName", childrenBean.getSeriesName());
        map.put("brandCode", childrenBean.getBrandCode());
        map.put("brandName", childrenBean.getBrandName());
        map.put("provinceCode", provinceCode);
        map.put("provinceName", provinceName);
        map.put("cityName", cityName);
        if (!TextUtils.isEmpty(childrenBean.getImageUrl())) {
            map.put("imageUrl", childrenBean.getImageUrl());
        } else {
            map.put("imageUrl", "");
        }
        view.showProgress("正在估价");
        model.queryValuationQuery(context, G.getHeader(context), map, new MyObserver<Result<ValuationQuery>>() {
            @Override
            public void onNext(Result<ValuationQuery> result) {
                if (G.REQUEST_OK == result.getCode()) {
                    ValuationQuery data = result.getData();
                    view.loadSuccess(data);
                } else {
                    view.showMessage(result.getMessage());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.hideProgress();
                view.showMessage("获取数据失败");
            }
        });
    }

    public void getBanner(Context context) {
        model.getBannerData(context, G.getHeader(context), new MyObserver<Results<VehicleSeries>>() {
            @Override
            public void onNext(Results<VehicleSeries> result) {
                super.onNext(result);
                if (result.getCode() == G.REQUEST_OK) {
                    view.setData(result.getData());
                } else {
                    view.showMessage(result.getMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("获取导航栏数据错误");
            }

        });
    }

    public void getBasicConfig(Context context) {
        String cityExtension = (String) SPUtils.get(context, "city_extension", "");
        String cityValue = (String) SPUtils.get(context, "city_value", "");
        String faceExtension = (String) SPUtils.get(context, "faceExtension", "");
        if (!TextUtils.isEmpty(cityExtension) && !TextUtils.isEmpty(cityValue)) {
            view.setCityData(cityExtension.split(","), cityValue.split(","));
        }
        view.setFaceCheck(faceExtension);
    }
}
